<?php
/**
 * Redis 服务类.
 *
 * @package zb
 */
class RedisTool
{

    private static $_ins = null;
    private $wconf;
    private $rconf;
    private $wredis = null;
    private $rredis = null;
    private $prefix = '';
    private function __construct()
    {
        $config = Yaf_Registry::get('config')->database->redis->toArray();
        $this->wconf = $config['write'];
        $this->rconf = $config['read'];
        $this->prefix = $config['prefix'];
    }

    /**
     * return self
     */
    public static function getInstance()
    {
        if(!self::$_ins instanceof self)
        {
            self::$_ins  = new self();
        }
        return self::$_ins;
    }

    public function setPlus($key, $time)
    {
        $num = $this->get($key) ?: 0;
        $this->set($key, $num+1, $time); 
    }


    public function getWproxy()
    {
        try
        {
            if (!$this->wredis instanceof Tools_RedisDriver) {
                $this->wredis = new Tools_RedisDriver($this->wconf);
            }
            return $this->wredis;
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function getRproxy()
    {
        try
        {
            if (!$this->rredis instanceof Tools_RedisDriver)
            {
                $this->rredis = new Tools_RedisDriver($this->rconf);
            }
            return  $this->rredis;
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function get($key)
    {
        try
        {
            return $this->getRproxy()->get($key);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tget\t" . $ex->getMessage());
            return false;
        }
    }

    public function set($key, $value, $expire = 0)
    {
        try
        {
            return $this->getWproxy()->set($key, $value, $expire);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tset\t" . $ex->getMessage());
            return false;
        }
    }

    public function delete($key)
    {
        try{
            return $this->getWproxy()->delete($key);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tdelete\t" . $ex->getMessage());
            return false;
        }
    }

    public function mset($arr)
    {
        try
        {
            return $this->getWproxy()->mset($arr);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tmset\t" . $ex->getMessage());
            return false;
        }
    }

    public function mget($arr)
    {
        try
        {
            return $this->getRproxy()->mget($arr) ?: array();
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tmget\t" . $ex->getMessage());
            return array();
        }
    }

    public function zAdd($key, $value)
    {
        try
        {
            return $this->getWproxy()->zAdd($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tzadd\t" . $ex->getMessage());
            return false;
        }
    }

    public function zRevRange($key, $start, $end, $withScore = false)
    {
        try
        {
            return $this->getRproxy()->zRevRange($key, $start, $end, $withScore);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tzrevrange\t" . $ex->getMessage());
            return array();
        }
    }

    public function increment($key, $value = 1, $expire = 0)
    {
        try
        {
            $ret =  $this->getWproxy()->incrBy($key, $value);
            if($expire)
            {
                $this->getWproxy()->expire($key, $expire);
            }
            return $ret;
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tincrby\t" . $ex->getMessage());
            return false;
        }
    }

    public function decrement($key, $value = 1)
    {
        try
        {
            return $this->getWproxy()->decrBy($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tdecrby\t" . $ex->getMessage());
            return false;
        }
    }

    public function zincrby($key, $member, $increment = 1)
    {
        try
        {
            return $this->getWproxy()->zincrby($key, $increment, $member);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tzincrby\t" . $ex->getMessage());
            return false;
        }
    }

    public function keys($pattern)
    {
        try
        {
            return $this->getRproxy()->keys($pattern);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tkeys\t" . $ex->getMessage());
            return array();
        }
    }

    public function rpush($key, $value)
    {
        try
        {
            return $this->getWproxy()->rpush($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tget\t" . $ex->getMessage());
            return false;
        }
    }

    public function lpush($key, $value)
    {
        try
        {
            return $this->getWproxy()->lpush($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tget\t" . $ex->getMessage());
            return false;
        }
    }



    public function lrange($key, $s = 0, $e = -1)
    {
        try {
            return $this->getRproxy()->lrange($key, $s, $e);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function hgetall($key)
    {
        try {
            return $this->getRproxy()->hgetall($key);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }



    public function zRem($key, $value)
    {
        try
        {
            return $this->getWproxy()->zrem($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tset\t" . $ex->getMessage());
            return false;
        }
    }

    public function zScore($key, $value)
    {
        try
        {
            return $this->getRproxy()->zscore($key, $value);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tset\t" . $ex->getMessage());
            return false;
        }
    }

    public function zRevrangebyscore($key, $offset, $nums)
    {
        try
        {
            return $this->getRproxy()->zrevrangebyscore($key, $offset, $nums);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tset\t" . $ex->getMessage());
            return false;
        }
    }


    public function srandmember($key, $num)
    {
        try {
            return $this->getRproxy()->srandmember($key, $num);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    //无序集合类型
    public function sadd($key, $value)
    {
        try
        {
            return $this->getWproxy()->sadd($key, $value);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function srem($key, $value)
    {
        try
        {
            return $this->getWproxy()->srem($key, $value);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function sismember($key, $value)
    {
        try
        {
            return $this->getRproxy()->sismember($key, $value);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }

    public function smembers($key)
    {
        try
        {
            return $this->getRproxy()->smembers($key);
        }
        catch(Exception $ex)
        {
            return false;
        }
    }
    
    public function select($db)
    {
        try{
            return $this->getWproxy()->select($db);
        }
        catch(Exception $ex)
        {
            //LogSvc::getBizErrLog()->log("redis error\tdelete\t" . $ex->getMessage());
            return false;
        }
    }
    
    
    public function ttl($key){
        try {
            return $this->getWproxy()->ttl($key);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
             return false;
        }
    }
     public function incr($key){
        try {
            return $this->getWproxy()->incr($key);
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
             return false;
        }
    }
}
